package com.demo.rabbitmq.consumer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * 消费者自动ack
 */
@Slf4j
@Service
public class DirectConsumer {

    /**
     * 三个消费者订阅同一条queue时，RabbitMQ默认是轮询分配消息到订阅者
     * @param content 消息内容
     */
    @RabbitListener(id = "direct_queue_1", queues = "direct_queue")
    public void directExchange1(String content) {
        log.info("消费者1，消费消息内容：" + content);
    }

    @RabbitListener(id = "direct_queue_2", queues = "direct_queue")
    public void directExchange2(String content) throws InterruptedException {
        TimeUnit.SECONDS.sleep(1);
        log.info("消费者2，消费消息内容：" + content);
    }

    @RabbitListener(id = "direct_queue_3", queues = "direct_queue", ackMode = "AUTO")
    public void directExchange3(String content) throws Exception {
        TimeUnit.SECONDS.sleep(1);
        log.info("消费者3，消费消息内容：" + content);
    }
}
